<template>
  <view style="padding: 30rpx;">
    <button @click="callSetStorage()">setStorage</button>
    <button @click="callGetStorage()">获取缓存</button>
    <button @click="callStoInfo()" type="primary">获取字段列表</button>
    <button @click="callRemove()" type="warn" plain="true">删除</button>
    <button @click="callClear()" type="warn">清空</button>
  </view>
</template>

<script>
export default {
  data() {
    return {};
  },
  methods: {
    callSetStorage() {
      console.log("callSetStorage--> userName zhenglimin");
      uni.setStorage({
        key: "userName",
        data: "zhenglimin",
        success() {
          console.log("存储userName 成功回调");
        },
        fail() {
          console.error("存储userName 失败");
        }
      });
    },
    callGetStorage() {
      // 修正：获取缓存应使用 uni.getStorage，而非 uni.setStorage
      uni.getStorage({
        key: "userName",
        success: function(result) {
          console.log("获取本地缓存成功 result.data:" + result.data);
        },
        fail: function(e) {
          // 修正：失败回调参数用 e，而非未定义的 result
          console.error("获取本地缓存失败:" + JSON.stringify(e));
          console.error("获取本地缓存失败:" + e.errMsg);
        }
      });
    },
    callStoInfo() {
      console.log("callStoInfo-->");
      uni.getStorageInfo({
        success: function(result) {
          console.log("callStoInfo-->" + JSON.stringify(result));
          console.log("callStoInfo-->" + result.keys);
        }
      });
    },
    callRemove() {
      uni.removeStorage({
        key: "xingming",
        success: () => {
          console.log("removeStorage success>");
        },
        fail() {
          console.log("removeStorage fail>");
        }
      });
    },
    callClear() {
      uni.clearStorage();
      console.log("缓存已全部清空"); // 补充清空反馈日志
    }
  }
};
</script>

<style>
button {
  margin: 30rpx;
}
</style>